import re
import sys

def fix_image_paths(file_path):
    """修复Markdown文件中的图片路径"""
    try:
        # 读取文件内容
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 从文件路径中提取章节名
        import os
        chapter_name = os.path.splitext(os.path.basename(file_path))[0]
        
        # 修复图片路径：处理多种可能的路径格式
        # 处理pandoc生成的绝对路径格式
        old_path1 = f'chap-mds\\images\\{chapter_name}/'
        old_path2 = f'chap-mds\\images\\{chapter_name}\\'
        # 处理相对路径格式
        old_path3 = f'..\\images/{chapter_name}/'
        old_path4 = f'..\\images\\{chapter_name}\\'
        # 处理新的pandoc输出格式（images目录下的子目录）
        old_path5 = f'images/{chapter_name}/'
        old_path6 = f'images\\{chapter_name}/'
        old_path7 = f'images\\{chapter_name}\\'
        # 处理pandoc新版本的输出格式（直接在images/media/目录下）
        old_path8 = f'..\\chap-mds\\images/media/'
        old_path9 = f'..\\chap-mds\\images\\media\\'
        old_path10 = f'chap-mds\\images/media/'
        old_path11 = f'chap-mds\\images\\media\\'
        
        new_path = f'./images/{chapter_name}/media/'
        
        fixed_content = content.replace(old_path1, new_path)
        fixed_content = fixed_content.replace(old_path2, new_path)
        fixed_content = fixed_content.replace(old_path3, new_path)
        fixed_content = fixed_content.replace(old_path4, new_path)
        fixed_content = fixed_content.replace(old_path5, new_path)
        fixed_content = fixed_content.replace(old_path6, new_path)
        fixed_content = fixed_content.replace(old_path7, new_path)
        fixed_content = fixed_content.replace(old_path8, new_path)
        fixed_content = fixed_content.replace(old_path9, new_path)
        fixed_content = fixed_content.replace(old_path10, new_path)
        fixed_content = fixed_content.replace(old_path11, new_path)
        
        # 处理复杂的路径问题
        # 修复各种形式的多余../前缀和./组合
        fixed_content = re.sub(r'\.\.[\/\\]\.\.[\/\\]\.\.[\/\\]', './', fixed_content)
        fixed_content = re.sub(r'\.\.[\/\\]\.\.[\/\\]', './', fixed_content)
        fixed_content = re.sub(r'\.\.[\/\\]\.[\/\\]\.\.[\/\\]\.[\/\\]', './', fixed_content)
        fixed_content = re.sub(r'\.\.[\/\\]\.[\/\\]', './', fixed_content)
        # 修复重复的./组合
        fixed_content = re.sub(r'\.\.[\/\\]\.\.[\/\\]\.\.[\/\\]\.\.[\/\\]', './', fixed_content)
        fixed_content = re.sub(r'(\.[\/\\]){2,}', './', fixed_content)  # 匹配多个./组合
        # 修复重复的media目录
        fixed_content = re.sub(r'/media/media/media/', '/media/', fixed_content)
        fixed_content = re.sub(r'/media/media/', '/media/', fixed_content)
        fixed_content = re.sub(r'\\media\\media\\media\\', '/media/', fixed_content)
        fixed_content = re.sub(r'\\media\\media\\', '/media/', fixed_content)
        
        # 写回文件
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(fixed_content)
        
        print(f"已成功修复 {file_path} 中的图片路径")
        return True
        
    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {e}")
        return False

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("用法: python fix_image_paths.py <markdown_file_path>")
        sys.exit(1)
    
    file_path = sys.argv[1]
    fix_image_paths(file_path)